package com.clouds.wms.pur;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.bean.Purmc;
import com.clouds.wms.bean.Purmd;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.PurmcService;
import com.clouds.wms.service.PurmdService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;

@Controller
@RequestMapping("/purmd")
public class PurmdController {
	
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="purmcService")
	private PurmcService purmcService;
	@Resource(name="purmdService")
	private PurmdService purmdService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;
	@RequestMapping(value = "/purmdMain.xhtml")
	public String purmdMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mcaa = RequestHelper.getString(request, "mcaa");
		String mcab = RequestHelper.getString(request, "mcab");
		Purmc purmc = purmcService.getPurmc(mcaa, mcab);
		model.addAttribute("mcaa", mcaa);
		model.addAttribute("mcab", mcab);
		model.addAttribute("purmc", purmc);
//		model.addAttribute("allCmsmjList", cmsmjService.getCmsmjListByMjac(SysParams.DOCUMENT_CATEGORY_CGDD));
		return "pur/purmd";
	}
	@RequestMapping(value = "/purmdList.xhtml")
	public String purmdList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mcaa = RequestHelper.getString(request, "mcaa");
		String mcab = RequestHelper.getString(request, "mcab");
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = purmdService.getPurmdList(mcaa, mcab, pageNumber, pageSize);
		List<Purmd> list = (List<Purmd>) pb.getData();
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/addPurmd.xhtml", method = RequestMethod.POST)
	public String addPurmc(HttpServletRequest request, HttpServletResponse response, Model model, Purmd purmd) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmd.setMdac(purmdService.getMaxMdac());
		purmdService.savePurmd(purmd);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + purmd.getMdab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 更新采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updatePurmd.xhtml", method = RequestMethod.POST)
	public String updatePurmc(HttpServletRequest request, HttpServletResponse response, Model model, Purmd purmd) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmdService.updatePurmd(purmd);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("更新了编号为[" + purmd.getMdab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delPurmd.xhtml")
	public String delPurmd(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mdaa = RequestHelper.getString(request, "mdaa");
		String mdab = RequestHelper.getString(request, "mdab");
		String mdac = RequestHelper.getString(request, "mdac");
		StringBuffer mdacs = new StringBuffer();
		for(String s : mdac.split(",")){
			mdacs.append("'").append(s).append("',");
		}
		mdacs = mdacs.delete(mdacs.length()-1, mdacs.length());
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmdService.delPurmd(mdaa, mdab, mdacs.toString());
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + mdaa+","+mdab+","+mdac + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delPurmdAll.xhtml")
	public String delPurmdAll(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mdaa = RequestHelper.getString(request, "mdaa");
		String mdab = RequestHelper.getString(request, "mdab");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmdService.delPurmdAll(mdaa, mdab);
		out.print("success");
		return null;
	}
	/**
	 * 变更采购单状态
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/auditPurmd.xhtml", method = RequestMethod.POST)
	public String auditPurmd(HttpServletRequest request, HttpServletResponse response, Model model, Purmd purmd) throws SysException, IOException {
		String mdaa = RequestHelper.getString(request, "mcaa");
		String mdab = RequestHelper.getString(request, "mcab");
		String status = RequestHelper.getString(request, "flag");
//		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmdService.changeStatus(mdaa, mdab, status);
		
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delPurmcmd.xhtml", method = RequestMethod.POST)
	public String delPurmcmd(HttpServletRequest request, HttpServletResponse response, Model model, Purmd purmd) throws SysException, IOException {
		String mdaa = RequestHelper.getString(request, "mcaa");
		String mdab = RequestHelper.getString(request, "mcab");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmdService.delPurmcmd(mdaa, mdab);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + purmd.getMdab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	
}
